package com.manage.system.web.controller;

import com.manage.common.core.result.JsonResult;
import com.manage.common.core.valid.AddGroup;
import com.manage.common.core.valid.UpdateGroup;
import com.manage.common.log.annotation.OperateLog;
import com.manage.common.log.enums.BusinessType;
import com.manage.common.oss.OSSFactory;
import com.manage.common.oss.service.CloudStorageService;
import com.manage.common.permissions.annotation.HasPermissions;
import com.manage.system.api.request.oss.StorageAddOrEditParam;
import com.manage.system.api.request.oss.StorageConfigParam;
import com.manage.system.api.service.ISysStorageConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

@RestController
@RequestMapping("/storage-config")
@AllArgsConstructor
@Api(tags = "云存储配置管理", value = "SysStorageConfigController")
public class SysStorageConfigController {

    private final ISysStorageConfigService sysStorageConfigService;
    private final OSSFactory ossFactory;

    @ApiOperation("获取所有存储配置信息")
    @PostMapping("/findListPage")
    public JsonResult findListPage(@RequestBody StorageConfigParam storageConfigParam) {
        return sysStorageConfigService.findListPage(storageConfigParam);
    }

    @ApiOperation("添加存储配置信息")
    @PostMapping("/addStorageConfig")
    @HasPermissions("system:storage:config:add")
    @OperateLog(module = "云存储配置管理",title = "添加云存储配置", businessType = BusinessType.INSERT)
    public JsonResult addStorageConfig(@Validated({AddGroup.class}) @RequestBody StorageAddOrEditParam storageAddOrEditParam) {
        return sysStorageConfigService.addStorageConfig(storageAddOrEditParam);
    }

    @ApiOperation("更新云存储配置状态")
    @PutMapping("/changeStatus/{id}/{status}")
    @HasPermissions("system:storage:config:edit")
    @OperateLog(module = "云存储配置管理", title = "更新配置状态",businessType = BusinessType.UPDATE)
    public JsonResult changeStatus(@PathVariable("id") Long id,@PathVariable("status") Boolean status){
        return sysStorageConfigService.changeStatus(id,status);
    }

    @ApiOperation("批量删除云存储配置")
    @DeleteMapping("/batchDelStorageConfig")
    @HasPermissions("system:storage:config:remove")
    @OperateLog(module ="云存储配置管理",title = "删除云存储配置", businessType = BusinessType.DELETE)
    public JsonResult batchDelStorageConfig(@RequestBody List<Long> storageConfigIds) {
        return sysStorageConfigService.batchDelStorageConfig(storageConfigIds);
    }

    @ApiOperation("修改存储配置信息")
    @PutMapping("/editStorageConfig")
    @HasPermissions("system:storage:config:edit")
    @OperateLog(module = "云存储配置管理",title = "修改云存储配置", businessType = BusinessType.UPDATE)
    public JsonResult editStorageConfig(@Validated({UpdateGroup.class}) @RequestBody StorageAddOrEditParam storageAddOrEditParam) {
        return sysStorageConfigService.editStorageConfig(storageAddOrEditParam);
    }

    @ApiOperation("[Remote->Feign] 根据配置code获取云存储配置")
    @GetMapping("/findByConfigCode/{configCode}")
    public JsonResult findByConfigCode(@PathVariable("configCode") String configCode) {
        return sysStorageConfigService.findByConfigCode(configCode);
    }

    @GetMapping("/test/{configCode}")
    public void test(@PathVariable("configCode") String configCode) {
        CloudStorageService build = ossFactory.build(configCode);
        File file = new File("/Users/zhangqing/Documents/图片/杨过.jpeg");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            String upload = build.upload(fileInputStream, "test.jpeg");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
